﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;

using Jzy.Workflow.Node;

using System.Text;
using Frame.Lib.Web;
using Frame.Lib.Enums;
using Frame.Lib.Utilities;
using Frame.Lib.Core.Dept;
using Frame.Lib.Core.User;
using Frame.Lib.Common;


namespace Jzy.Web.WorkFlow.Design.Node
{
    public partial class Edit : System.Web.UI.Page
    {
        private string FlowID
        {
            get
            {
                return Request["FlowID"];
            }
        }
        private string PKID
        {
            get
            {
                return Request["PKID"];
            }
        }

        private FlowNodeBO _flowNode = new FlowNodeBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            CssFileManager.AddCssFile(CssFile.tab2);
            if (!IsPostBack)
            {
                BindList();
                BindDrp();                
                DispInfo();
                AddAttr();
                
                this.Page.Form.DefaultButton = btnSave.UniqueID;
            }
            //JSUtil.SetDivHeight(pnlMain.ClientID, 65);
            
        }

        private void DispInfo()
        {
            if (PKID == null)
            {
                Width.Text = "90";
                Height.Text = "55";
                Top.Text = "0";
                Left.Text = "200";
            }
            else
            {
                FlowNodeInfo info = _flowNode.GetInfo(PKID);
                EntityUtil.SetControl(pnlMain, info);
                if (info.SubmitMode == (int)SubmitMode.自动)
                {
                    chkSubmitMode.Checked = true;
                }

                hidRoleIDs.Value = StrUtil.MergeToStr(info.RoleIDs, ",");
                hidUserIDs.Value = DeptBO.MergeDeptUser(null, info.UserIDs);

                if (info.DefaultUserID != null)
                {
                    hidDefaultUserID.Value = info.DefaultUserID.ToString();
                    txtDefaultUserName.Text = new UserBO().GetName(info.DefaultUserID ?? 0);
                }
            }
        }

        private void AddAttr()
        {
            chkSubmitMode.Attributes.Add("onclick", "chk_click(this,'" + DefaultNextNodeID.ClientID + "');");
            IsParallel.Attributes.Add("onclick", "chk_click(this,'" + ParallelMode.ClientID + "');");

            if (chkSubmitMode.Checked == false)
            {
                DefaultNextNodeID.Attributes.Add("disabled", "disabled");
            }
            if (IsParallel.Checked == false)
            {
                ParallelMode.Attributes.Add("disabled", "disabled");
            }

            if ((FlowNodeType)int.Parse(TypeID.SelectedValue) == FlowNodeType.上级领导)
            {

                IAttributeAccessor[] ctls = new IAttributeAccessor[] { UserNames,  lnkSelectUser, RoleNames,  lnkSelectRole, txtDefaultUserName,  lnkSelectDefaultUser };
                foreach (IAttributeAccessor ctl in ctls)
                {
                    ctl.SetAttribute("disabled", "disabled");
                }              
            }

            JSUtil.ShowDeptUserTreeEx(hidUserIDs, UserNames, lnkSelectUser);

            JSUtil.ShowDlg(hidRoleIDs, RoleNames, "SelectRole.aspx", lnkSelectRole);

            string script = "showDefaultUser('" + hidDefaultUserID.ClientID + "','" + txtDefaultUserName.ClientID + "','" + hidRoleIDs.ClientID + "','" + hidUserIDs.ClientID + "');";
            txtDefaultUserName.Attributes.Add("onclick",script );
            lnkSelectDefaultUser.Attributes.Add("onclick", script);
        }

        private void BindDrp()
        {
            ListItem item = new ListItem(FlowNodeType.常规.ToString(), ((int)FlowNodeType.常规).ToString());
            item.Attributes.Add("onclick", "type_click(1)");
            TypeID.Items.Add(item);

            item = new ListItem(FlowNodeType.上级领导.ToString(), ((int)FlowNodeType.上级领导).ToString());
            item.Attributes.Add("onclick", "type_click(2)");
            TypeID.Items.Add(item);
            TypeID.SelectedIndex = 0;

            WebUtil.BindListControlWithEnum(ParallelMode, typeof(ParallelMode));

            
            List<FlowNodeInfo> nodes = _flowNode.GetNextNodes(this.PKID);
            if (nodes != null && nodes.Count > 0)
            {
                DefaultNextNodeID.DataSource = nodes;
                DefaultNextNodeID.DataTextField = "Name";
                DefaultNextNodeID.DataValueField = "ID";
                DefaultNextNodeID.DataBind();
            }                
            else
            {
                DefaultNextNodeID.Items.Add(new ListItem("无下一节点", "-1"));
            }
        }

        private void BindList()
        {
            List<EnumInfo> list = ConvertUtil.ConvertEnum(typeof(NodeTab));
            rpt.DataSource = list;
            rpt.DataBind();
            
        }

        //private void BindDefaultUser()
        //{
        //    string str = hidUserIDs.Value;
        //    List<int> lstUserIDs = new List<int>();
        //    DeptBO.SplitDeptUser(hidUserIDs.Value, null, lstUserIDs);

        //    UserBO user = new UserBO();
        //    List<UserInfo> list = user.GetList(lstUserIDs).ToList();
        //    List<UserInfo> list2 = user.GetListByRoleIDs(hidRoleIDs.Value);


        //    //需要去重
        //    list.AddRange(list2.Where(data => !(from tmp in list
        //                                        select tmp.ID).Contains(data.ID)).ToList());

        //    if (list.Count > 0)
        //    {
        //        DefaultUserID.DataSource = list;
        //        DefaultUserID.DataTextField = "Name";
        //        DefaultUserID.DataValueField = "ID";
        //        DefaultUserID.SelectedValue = null;
        //        DefaultUserID.DataBind();
        //    }
        //    else
        //    {
        //        DefaultUserID.Items.Clear();
        //    }
        //}

       

        protected void btnSave_Click(object sender, EventArgs e)
        {
            FlowNodeInfo info = _flowNode.GetInfo(PKID);
            EntityUtil.SetEntity(pnlMain, info);

            info.UserIDs = new List<int>();
            DeptBO.SplitDeptUser(hidUserIDs.Value, null, info.UserIDs);

            info.RoleIDs = StrUtil.SplitToInt(hidRoleIDs.Value);


            if (chkSubmitMode.Checked)
            {
                info.SubmitMode = (int)SubmitMode.自动;
            }
            else
            {
                info.SubmitMode = (int)SubmitMode.手动;
            }

            if (PKID == null)
            {
                info.FlowID = int.Parse(FlowID);
            }

            if (hidDefaultUserID.Value != "")
            {
                info.DefaultUserID = int.Parse(hidDefaultUserID.Value);
            }
                        

            _flowNode.Save(info);

            JSUtil.RefreshView();

            //StringBuilder sbJS = new StringBuilder();
            //sbJS.Append("<script language='javascript'>\n");
            //sbJS.Append("returnValue=true;\n");            
            //sbJS.Append("window.close();\n");
            //sbJS.Append("</script>");
            //Response.Write(sbJS.ToString());
            //Response.End();
        }
    }
}
